package com.cn.ccp.action.onlineEdu;

import java.io.File;
import java.util.List;

import org.apache.log4j.Logger;

import com.cn.ccp.action.BaseAction;
import com.cn.ccp.bean.CourseBean;
import com.cn.ccp.bean.SelectedCourse;
import com.cn.ccp.util.CCPContants;


/**
 * 老师在线授课
 * @author renyongcheng
 *
 */
public class OnlineTeach extends BaseAction{
	private Logger log = Logger.getLogger(OnlineTeach.class);
	private String strCourseList;//当前学习课程列表
	private int totalNumber;
	private String teachId;//老师id
	
	public OnlineTeach(){
		log.info("---------OnlineTeach()-------");
		teachId = String.valueOf(CCPContants.getUserBean().getId());
		log.info("------------------teachId = "+teachId);
		getpagedetail();
	}
	
	public void getpagedetail() {
		totalNumber = getCourseService().onlineTeachCount(teachId);
		log.info("------------------totalNumber = "+totalNumber);
		setItemCount(totalNumber);
		int currentpage = getCurrentPage();
		int pagesize =8;
		this.setPageSize(pagesize);
		int start = (currentpage - 1) * pagesize;
		List<CourseBean> list = getCourseService().onlineTeach(teachId, start, pagesize);
		log.info("------------------list.size() = "+list.size());
		StringBuffer buf = new StringBuffer();
		int i = 0;
		buf.append("<ul class='thumbnails'>");
		for(CourseBean b : list){
//			CCPContants.getFilePath(b.getImage() , getFilePathService(),b.getFile_path_id() , CCPContants.getUserBean().getId() ,false,null);
			if(i % 4 == 0 && i != 0){
				buf.append("</ul>");
				buf.append("<ul class='thumbnails'>");
			}
			buf.append("<li class='span3'>");
			
			if(!b.getImage().equals("book-default.png")){
				File sourseFile = null;
				if(b.getFile_path_id() !=  -1)
					sourseFile = new File(getFilePathService().findFilePathById(b.getFile_path_id()).getPath()+b.getImage());
				else
					sourseFile = new File(CCPContants.SYS_PATH+"files"+File.separator + b.getImage());
				
				File des = new File(CCPContants.SYS_PATH + "tempfile"+File.separator+CCPContants.getUserBean().getId() + File.separator);
				if(des.mkdir()){
					des.mkdirs();
				}
				des = new File(CCPContants.SYS_PATH + "tempfile"+File.separator+CCPContants.getUserBean().getId()  + File.separator +sourseFile.getName());
				CCPContants.copyFile(sourseFile.getPath(), des.getPath());
				b.setImage(CCPContants.getCurrUserId()+"/"+des.getName());
//				b.setImage(CCPContants.getUserBean().getId()+"/"+fileName);
			}
			log.info(b.getImage());
//			if(b.isHasSelected())
//				buf.append("<img src='tempfile/"+(b.getImage() == null ? "book-default.png'":b.getImage()) +"'  style=\"width:190px;height:156px;\"  alt='' >");
//			else	
			buf.append("<a href='pages/course/select/editSelect.faces?courseId="+b.getId()+"&onlineTeach=true' class='img'><img src='tempfile/"+(b.getImage() == null ? "book-default.png'":b.getImage()) +"'  style=\"width:190px;height:156px;\"  alt='' ></a>");
			buf.append("<div class='caption'>");
//			log.info("---------"+b.getCourse_name());
			buf.append("<p title=\""+b.getCourse_name()+"\">");
			buf.append(b.getCourse_name().length() > 9 ? b.getCourse_name().substring(0, 9)+ "...<br />" : b.getCourse_name()+"<br />");
			buf.append(getMessages("online_page_select_teacher")+b.getTeacherName()+"<br />");
			if(!b.getStart_level().equalsIgnoreCase("0"))
				buf.append("<span class='text-error'>"+b.getStart_level()+"</span>"+getMessages("online_page_select_star")+"<br />");
			else
				buf.append("<span class='text-error'></span>"+getMessages("online_page_select_star_level_no")+"<br />");
			buf.append("<span class='text-info'>"+b.getMarks()+"</span>"+getMessages("online_page_select_reply")+"<br />");
			buf.append("</p>");
			buf.append("</div>");
//			if(b.isHasSelected())
//				buf.append("&nbsp;&nbsp;<i class ='btn'>"+ getMessages("online_page_select_studing") +"</i>&nbsp;&nbsp;");	
//			else
//				buf.append("&nbsp;&nbsp;<a href='pages/course/select/editSelect.faces?courseId="+b.getId()+"' class='btn btn-primary'>&nbsp;"+getMessages("online_page_select_action_browse")+"</a>&nbsp;&nbsp;");
//			buf.append("<a href='pages/course/studentCourseIssue.faces?id="+b.getId()+"' class='btn'>&nbsp;&nbsp;"+getMessages("online_page_select_action_quest")+"&nbsp;</a><br/>");
//			buf.append("<span class='muted'>"+b.getStudyUsers()+getMessages("online_page_select_person_studing")+"</span>");
			buf.append("</li>");
			i++;
		}
		buf.append("</ul>");
		strCourseList = buf.toString();
	}
	
	
	public String getStrCourseList() {
		return strCourseList;
	}

	public int getTotalNumber() {
		return totalNumber;
	}

	public void setTotalNumber(int totalNumber) {
		this.totalNumber = totalNumber;
	}
	
}
